      Subroutine INITSTATE

      include "param.fi"
      include "common.fi"

      Integer FNUM, I
      Character*2 TMPP

      If (IfContinue .EQ.1) Then
          ! Read vtk file
          Write(*,*) "Reading file ", InitFileName

          Call BORROW_FREEFILE(FNUM)
          Open(FNUM, FILE=InitFileName)

          ! Location
          Do I = 1, 5
            Read(FNUM,*)
          Enddo
          Do I = 1, NTOT3T
            Read(FNUM,*) X1(I), X2(I), X3(I)
          Enddo

          ! Skip the radius
          Do I = 1, 3 + NTOT3T
            Read(FNUM,*)
          Enddo

          ! Skip the index
          Do I = 1, 2 + NTOT3T
            Read(FNUM,*)
          Enddo

          ! Weight
          Do I = 1, 2
            Read(FNUM,*)
          Enddo
          Do I = 1, NTOT3T
            Read(FNUM,*) Weight(I)

            If (tagRegroup .EQ. 1 .AND. DIMM.EQ.DIMM2D) Then
              ShapeP(I,1) = Sqrt(Weight(I))
              ShapeP(I,3) = Sqrt(Weight(I))
            else if (tagRegroup .EQ. 2 .AND. DIMM.EQ.DIMM2D) Then
              ShapeP(I,1) =0.5*Sqrt(Weight(I))
              ShapeP(I,3) =0.5*Sqrt(Weight(I))
              ShapeVec2D(I,1)=1
              ShapeVec2D(I,2)=0
              ShapeVec2D(I,3)=0
              ShapeVec2D(I,4)=1
            Endif
          Enddo

          ! Pressure
          Do I = 1, 2
            Read(FNUM,*)
          Enddo
          Do I = 1, NTOT3T
            Read(FNUM,*) P(I)
          Enddo

          ! Velocity
          Do I = 1, 1
            Read(FNUM,*)
          Enddo
          Do I = 1, NTOT3T
            Read(FNUM,*) U1(I), U2(I), U3(I)
          Enddo

          ! Skip the momentum
          Do I = 1, 1 + NTOT3T
            Read(FNUM,*)
          Enddo

          ! density
          Do I = 1, 2
            Read(FNUM,*)
          Enddo
          Do I = 1, NTOT3T
            Read(FNUM,*) RHO(I)
            RHO(I) = RHO(I) / Weight(I)
          Enddo

          Close(FNUM)
          Call RETURN_FREEFILE(FNUM)

          Call PressureField
      ElseIf (IfContinue .EQ.2) Then
          ! Read vtk file
          Write(*,*) "Reading file ", InitFileName

          Call BORROW_FREEFILE(FNUM)
          Open(FNUM, FILE=InitFileName)

          Read(FNUM, *), TMPP, iSTEP, T
          STEP = iSTEP
          Read(FNUM,*)

          DO I = 1, NTOT3T
            If (DIMM .EQ. DIMM3D) Then
              Read(FNUM, *) X1(I), X2(I), X3(I), U1(I), U2(I), U3(I),
     &                    Rho(I), P(I)
            Else
              Read(FNUM, *) X1(I), X3(I), U1(I), U3(I),
     &                    Rho(I), P(I), VORTEX(I), Weight(I)
              X2(I) = 0.0
              U2(I) = 0.0
            Endif
          Enddo

          Close(FNUM)
          Call RETURN_FREEFILE(FNUM)
      Endif


      End Subroutine

C==================================================================
      Subroutine RESULTS
      include "param.fi"
      include "common.fi"

      Integer FNUM, I
      Character*40 FM

      Call STATISTICS1

      Write(*,*) "WRITING RESULTS ..."
      FM = 'res                 '
      CALL CONCATI5(FM, iSTEP)
      Call BORROW_FREEFILE(FNUM)
      Open(FNUM, FILE=FM)
      Write(FNUM, '(A2,I6,E18.7)'), "# ", iSTEP, T
      If (DIMM .EQ. DIMM3D) Then
        Write(FNUM, *) "ZONE I=",NTOT,",J=",NTOT,",K=",NTOT
      Else
        Write(FNUM, *) "ZONE I=",NP,",J=",NP
      Endif
      DO I = 1, NTOT3T
        If (DIMM .EQ. DIMM3D) Then
          Write(FNUM, 1234) X1(I), X2(I), X3(I), U1(I), U2(I), U3(I),
     &                    Rho(I), P(I)
        Else
          Write(FNUM, 1234) X1(I), X3(I), U1(I), U3(I),
     &                    Rho(I), P(I), VORTEX(I), Weight(I)
        Endif
      Enddo
      Close(FNUM)
      Call RETURN_FREEFILE(FNUM)
1234  Format(19E18.9)
      End Subroutine



